package life.sweet.community.controller;

import life.sweet.community.dto.FileDTO;
import life.sweet.community.provider.AliyunOSSProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Controller
public class FileController {
    @Autowired
    private AliyunOSSProvider aliyunOSSProvider;

    @RequestMapping("/file/upload")
    @ResponseBody
    public FileDTO upload(HttpServletRequest request) {
        MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest)request;
        MultipartFile file = fileRequest.getFile("editormd-image-file");
        String url = aliyunOSSProvider.upload(file);
        FileDTO fileDTO = new FileDTO();
        fileDTO.setSuccess(1);
        fileDTO.setMessage("上传成功！");
        fileDTO.setUrl(url);
        return fileDTO;
    }
}
